package com.chr.hongyun.service.impl;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONArray;
import com.chr.hongyun.dao.TbMeetingDao;
import com.chr.hongyun.exception.HongyunException;
import com.chr.hongyun.pojo.TbMeeting;
import com.chr.hongyun.service.MeetingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * 创建日期:2023/4/30
 * 创建人:陈浩然
 */
@Service
@Slf4j
public class MeetingServiceImpl implements MeetingService {

    @Autowired
    private TbMeetingDao meetingDao;

    @Override
    public void insertMeeting(TbMeeting entity) {
        int row = meetingDao.insertMeeting(entity);
        if (row != 1) {
            throw new HongyunException("会议添加失败");
        }
    }

    @Transactional
    @Override
    public ArrayList<HashMap> searchMyMeetingListByPage(HashMap param) {
        ArrayList<HashMap> list = meetingDao.searchMyMeetingListByPage(param);
        String date = null;
        ArrayList resultList = new ArrayList();
        HashMap resultMap = null;
        JSONArray array = null;
        for (HashMap map : list) {
            String temp = map.get("date").toString();
            Long currentTimeToStart = (Long) map.get("currentTimeToStart");
            Long currentTimeToEnd = (Long) map.get("currentTimeToEnd");
            Integer status = (Integer) map.get("status");
            if (currentTimeToStart > 0 && currentTimeToEnd < 0) {
                HashMap info = new HashMap();
                BigInteger meetingId = (BigInteger) map.get("id");
                status = 2;
                info.put("meetingId", meetingId);
                info.put("status", status);
                meetingDao.updateMeetingStatusById(info);
                map.replace("status", status);
            } else if (currentTimeToEnd > 0 && status != 3) {
                HashMap info = new HashMap();
                BigInteger meetingId = (BigInteger) map.get("id");
                status = 3;
                info.put("meetingId", meetingId);
                info.put("status", status);
                meetingDao.updateMeetingStatusById(info);
                map.replace("status", status);
            }
            if (!temp.equals(date)) {
                date = temp;
                resultMap = new HashMap();
                resultMap.put("date", date);
                array = new JSONArray();
                resultMap.put("list", array);
                resultList.add(resultMap);
            }
            array.put(map);
        }
        return resultList;
    }

    @Override
    public HashMap searchMeetingById(int meetingId) {
        HashMap map = meetingDao.searchMeetingById(meetingId);
        ArrayList<HashMap> memberList = meetingDao.searchMeetingMembers(meetingId);
        map.put("members", memberList);
        return map;
    }

    @Override
    public void updateMeetingInfo(HashMap param) {
        int row = meetingDao.updateMeetingInfo(param);
        if (row != 1) {
            throw new HongyunException("会议更新失败");
        }
    }

    @Override
    public void deleteMeetingById(int id) {
        HashMap meeting = meetingDao.searchMeetingById(id);
        DateTime date = DateUtil.parse(meeting.get("date") + " " + meeting.get("start"));
        DateTime now = DateUtil.date();
        if (now.isAfterOrEquals(date.offset(DateField.MINUTE, -20))) {
            throw new HongyunException("距离会议开始不足20分钟，不能删除会议");
        }
        int row = meetingDao.deleteMeetingById(id);
        if (row != 1) {
            throw new HongyunException("会议删除失败");
        }
    }

}
